package base.controller;

import base.bean.CheckinEntity;
import base.bean.RoomEntity;
import base.service.ICheckService;
import base.service.IRoomService;
import base.utils.ChangeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * Created by 30509 on 2018/3/18.
 */
@Controller
@RequestMapping("/test")
public class LiveController {
    @Autowired
    IRoomService iRoomService;

    @RequestMapping("/roomcheckin")
    private ModelAndView roomCheckIn(){
        ModelAndView checkIn=new ModelAndView();
        List<RoomEntity> list = iRoomService.findByaEquals();
        checkIn.addObject("list", list);
        checkIn.setViewName("roomcheckin");
        return checkIn;
    }

    @RequestMapping(value = "/queryx", method = RequestMethod.GET)
    private ModelAndView queryaction(HttpServletRequest request)  {
        ModelAndView queryAction=new ModelAndView();
        String type=request.getParameter("type");
        List<RoomEntity> roomType = iRoomService.findCipsAndRoomtype(type);
        if(type != null && !"".equals(type)){
            queryAction.addObject("list", roomType);
            queryAction.setViewName("roomcheckin");
            return queryAction;
        }else {
        return roomCheckIn();
    }}

    @RequestMapping("/checkinroom")
    private ModelAndView checkInRoom(HttpServletRequest request){
        ModelAndView queryForUpdate=new ModelAndView();
        String idd=request.getParameter("id");
        int id= ChangeUtils.parseInt(idd, -1);
        RoomEntity room = iRoomService.findOne(id);
        queryForUpdate.addObject("qt", room);
        queryForUpdate.setViewName("checkinroom");
        return  queryForUpdate;
    }

    @Autowired
    ICheckService iCheckService;
    @RequestMapping(value = "/checkinroomx",method =RequestMethod.POST)
    private ModelAndView checkIn(HttpServletRequest  request){
        String idd=request.getParameter("id");
        int id= ChangeUtils.parseInt(idd, 0);
        String roomid =request.getParameter("roomid");
        int roomidd= ChangeUtils.parseInt(roomid,0);
        String price=request.getParameter("price");
        String datein =request.getParameter("datein");
        String dateout =request.getParameter("dateout");
        String personname=request.getParameter("personname");
        String cardtype =request.getParameter("cardtype");
        String cardid=request.getParameter("cardid");
        String personsex=request.getParameter("personsex");
        String phone=request.getParameter("phone");
        String nop =request.getParameter("numofpersons");
        int nops= ChangeUtils.parseInt(nop, -1);
        String clp =request.getParameter("checkinpersons");
        int clps = ChangeUtils.parseInt(clp,-1);
        String cip=request.getParameter("cips");
        int cips = ChangeUtils.parseInt(cip,-1);
        CheckinEntity ct=new CheckinEntity();
        ct.setRoomid(roomidd);
        ct.setPrice(price);
        ct.setDatein(datein);
        ct.setDateout(dateout);
        ct.setPersonname(personname);
        ct.setCardtype(cardtype);
        ct.setCardid(cardid);
        ct.setSex(personsex);
        ct.setCips(cip);
        ct.setPhone(phone);
        iCheckService.save(ct);
       RoomEntity dd=new RoomEntity();
        iRoomService.updateNameById(cips,id);
        /*cir.setViewName(a);*/
        return roomCheckIn();
    }

}
